/**
 * 推广渠道管理管理初始化
 */
var VpnChannelInfo = {
    id: "VpnChannelInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
VpnChannelInfo.initColumn = function () {
    return [[
        {title: '渠道编号', field: 'id',  align: 'center',width:100,
            templet:function (res) {
                return "000"+res.id;
            }},
        {title: '渠道名称', field: 'channelName',  align: 'center',width: 140},
        {title: '创建时间', field: 'createTime',  align: 'center',width: 130,
            templet:function (res) {
                return new Date(res.createTime).format("yyyy-MM-dd");
            }
        },
        {title: '结束时间', field: 'endTime', align: 'center',width: 130,
            templet:function (res) {
                if (!$.trim(res.endTime)){
                    return "";
                }else{
                    return new Date(res.endTime).format("yyyy-MM-dd");
                }
            }
        },
        {title: '链接', field: 'promoteUrl', align: 'center',width: 400},
        {title: '状态', field: 'status', align: 'center',width: 90,
            templet:function (res) {
                if (res.status === 1){
                    return '<div style="color: #336d3c">已生效</div>';
                }else{
                    return '<div style="color: #bd0404">已停用</div>';
                }
            }
        },
        {title: '操作', field: 'id', align: 'center',width: 150,
            templet:function (res) {
                if (res.status === 1){
                    return "<div style='display: flex;flex-direction: row;align-items: center'>" +
                        "<div style='color: #bd0404;margin-right: 10px;' onclick=\"VpnChannelInfo.updateChannelStatus('"+res.id+"','1')\">停用</div>" +
                        "|<div style='color: #0d8ddb;margin-left: 10px;' onclick=\"VpnChannelInfo.copyChannelPromoteUrl('"+res.promoteUrl+"')\">复制链接</div>" +
                        "</div>";
                }else{
                    return "";
                }
            }
        }
    ]];
};

/**
 * 点击添加推广渠道管理
 */
VpnChannelInfo.openAddVpnChannelInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加推广渠道',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnChannelInfo/vpnChannelInfo_add'
    });
    this.layerIndex = index;
};

/**
 * 查询推广渠道管理列表
 */
VpnChannelInfo.search = function () {
    let queryData = {};
    queryData['channelName'] = $("#channelName").val();
    queryData['channelStatus'] = $("#channelStatus").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();

    VpnChannelInfo.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        },
        where: queryData
    });
};

/**
 * 复制推广链接
 */
VpnChannelInfo.copyChannelPromoteUrl = function(promoteUrl){
    let save = function (e){
        e.clipboardData.setData('text/plain',promoteUrl);//clipboardData对象
        e.preventDefault();//阻止默认行为
    };
    document.addEventListener('copy',save);
    document.execCommand("copy");
    Feng.success("链接复制成功!");
}

/**
 * 修改渠道状态
 */
VpnChannelInfo.updateChannelStatus = function(channelId,channelStatus){
    console.log(channelStatus);
    let updateStatus = channelStatus;
    let hintStr = "确定进行此操作吗？";
    if (channelStatus == 1){
        updateStatus = 0;
        hintStr = "确定停用该渠道吗？"
    }else{
        updateStatus = 1;
        hintStr = "确定重新启用该渠道吗？"
    }


    let loadIndex;
    let confirmIndex = layer.confirm(hintStr,
        {btn:['确认停用','取消']},
        function () {
            layer.closeAll()
            loadIndex = layer.load(0,{
                shade:false
            });
            setTimeout(function () {
                updateStatusOfNetwork(channelId,updateStatus);
            },1000);
        },function () {
            layer.msg("操作已取消",{icon:1})
        });
}


function updateStatusOfNetwork(channelId,updateStatus){
    let formData = new FormData;
    formData.append("channelId",channelId);
    formData.append("status",updateStatus);

    $.ajax({
        url: Feng.ctxPath + "/vpnChannelInfo/updateChannelStatus",
        dataType:'json',
        type:'POST',
        async: false,
        data: formData,
        processData : false, // 使数据不做处理
        contentType : false, // 不要设置Content-Type请求头
        success: function(data){
            layer.closeAll();
            if(data.code == 200){
                Feng.success("状态修改成功!");
                VpnChannelInfo.refresh();
            }
        },
        error:function(response){
            Feng.error(response.responseJSON.message);
            layer.closeAll();
        }
    });
}

/**
 * 刷新列表
 */
VpnChannelInfo.refresh = function(){
    VpnChannelInfo.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        }
    });
}

VpnChannelInfo.tableList = function(queryData){
    layui.use("table",function () {
        var table = layui.table;

        VpnChannelInfo.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath +"/vpnChannelInfo/list",
            cellMinWidth: 100,
            where: queryData,
            page: true,
            limit: 20,
            cols: VpnChannelInfo.initColumn(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

$(function () {
    DateUtils.expandDate();
    VpnChannelInfo.tableList();
});
